#include "serialsettingsdialog.h"
#include "ui_serialsettingsdialog.h"

SerialSettingsDialog::SerialSettingsDialog(QString &name, PortSettings *settings, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SerialSettingsDialog)
{
    ui->setupUi(this);

    mName = name;
    mpSettings = settings;

#ifdef  Q_OS_WIN
    foreach (QextPortInfo info, QextSerialEnumerator::getPorts()) {
        ui->portBox->addItem(info.portName);
    }
#else
    ui->portBox->addItem("ttyO1");
    ui->portBox->addItem("ttyO2");
    ui->portBox->addItem("ttyO4");
#endif
    ui->portBox->setCurrentIndex(ui->portBox->findText(mName));
    //make sure user can input their own port name!
    ui->portBox->setEditable(true);

    ui->baudRateBox->addItem("1200", BAUD1200);
    ui->baudRateBox->addItem("2400", BAUD2400);
    ui->baudRateBox->addItem("4800", BAUD4800);
    ui->baudRateBox->addItem("9600", BAUD9600);
    ui->baudRateBox->addItem("19200", BAUD19200);
    ui->baudRateBox->addItem("38400", BAUD38400);
    ui->baudRateBox->addItem("57600", BAUD57600);
    ui->baudRateBox->addItem("115200", BAUD115200);
    ui->baudRateBox->setCurrentIndex(ui->baudRateBox->findText(QString::number(mpSettings->BaudRate)));

    ui->dataBitsBox->addItem("5", DATA_5);  //5
    ui->dataBitsBox->addItem("6", DATA_6);  //6
    ui->dataBitsBox->addItem("7", DATA_7);  //7
    ui->dataBitsBox->addItem("8", DATA_8);  //8
    ui->dataBitsBox->setCurrentIndex(mpSettings->DataBits - DATA_5);

    ui->parityBox->addItem("NONE", PAR_NONE);   //0
    ui->parityBox->addItem("ODD", PAR_ODD);     //1
    ui->parityBox->addItem("EVEN", PAR_EVEN);   //2
    ui->parityBox->setCurrentIndex(mpSettings->Parity - PAR_NONE);

    ui->stopBitsBox->addItem("1", STOP_1);  //0
    ui->stopBitsBox->addItem("2", STOP_2);  //1 or 2
    ui->stopBitsBox->setCurrentIndex((STOP_1 == mpSettings->StopBits) ? 0 : 1);

    ui->flowControlBox->addItem("OFF", FLOW_OFF);           //0
    ui->flowControlBox->addItem("HARDWARE", FLOW_HARDWARE); //1
    ui->flowControlBox->addItem("XONXOFF", FLOW_XONXOFF);   //2
    ui->flowControlBox->setCurrentIndex(mpSettings->FlowControl - FLOW_OFF);

    QextSerialEnumerator *enumerator = new QextSerialEnumerator(this);
    enumerator->setUpNotifications();
    connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
    connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
}

SerialSettingsDialog::~SerialSettingsDialog()
{
    delete ui;
}

QString SerialSettingsDialog::name()
{
    return mName;
}

void SerialSettingsDialog::accept()
{
    QString name = ui->portBox->currentText();
    if (name.length()) {
        mName = name;
        mpSettings->BaudRate = (BaudRateType)ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt();
        mpSettings->DataBits =(DataBitsType)(ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());
        mpSettings->Parity = (ParityType)ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt();
        mpSettings->StopBits = (StopBitsType)(ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());
        mpSettings->FlowControl = (FlowType)(ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());

        QDialog::accept();
    }
}

void SerialSettingsDialog::onPortAddedOrRemoved()
{
    QString current = ui->portBox->currentText();

    ui->portBox->blockSignals(true);
    ui->portBox->clear();
    foreach (QextPortInfo info, QextSerialEnumerator::getPorts()) {
        ui->portBox->addItem(info.portName);
    }
    ui->portBox->setCurrentIndex(ui->portBox->findText(current));
    ui->portBox->blockSignals(false);
}
